﻿using EFTest.EF;
using EFTest.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EFTest.Controllers
{
    public class HomeController : ControllerBase
    {
        private readonly ILogger<HomeController> _logger;
        private readonly TestDbContext _testDbContext;

        public HomeController(ILogger<HomeController> logger, TestDbContext testDbContext)
        {
            _logger = logger;
            _testDbContext = testDbContext;
        }

        public void Index()
        {
            //新增一条数据
            _testDbContext.Add(new User { UserName = "admin_test" });
            //从数据库查询，并修改UserName字段的值，但不显式提交修改
            var model = _testDbContext.Users.FirstOrDefault(m => m.Id == 1);
            model.UserName = "我的测试22";
            //保存
            _testDbContext.SaveChanges();
        }
    }
}
